﻿using Jacobi.Vst.Core;
using Jacobi.Vst.Framework;
using Levfier.GUI;
using Levfier.Proccessors;
using System;
using System.Drawing;

namespace Levfier
{
    /// <summary>
    /// Implements the custom UI editor for the plugin.
    /// </summary>
    class PluginEditor : IVstPluginEditor
    {
        /// <summary>
        /// 画面幅
        /// </summary>
        private static readonly int Width = 800;

        /// <summary>
        /// 画面高さ
        /// </summary>
        private static readonly int Height = 600;

        /// <summary>
        /// プラグインインターフェース
        /// </summary>
        private FxPluginInterfaceManager _plugin;

        /// <summary>
        /// 画面のラッパー
        /// </summary>
        private WpfControlWrapper<MainWindow> wpfWrapper;

        /// <summary>
        /// Constructs a new instance.
        /// </summary>
        /// <param name="plugin">Must not be null.</param>
        public PluginEditor(FxPluginInterfaceManager plugin)
        {
            _plugin = plugin;
            if (null == wpfWrapper)
            {
                wpfWrapper = new WpfControlWrapper<MainWindow>(Width, Height);
                _plugin.PluginRoot.Window = wpfWrapper.Instance;
            }
        }

        #region IVstPluginEditor Members

        public Rectangle Bounds
        { 
            get 
            {
                Rectangle rect;
                wpfWrapper.GetBounds(out rect);
                return rect; 
            }
        }

        public void Close()
        {
            wpfWrapper.Close();
        }

        public void KeyDown(byte ascii, VstVirtualKey virtualKey, VstModifierKeys modifers)
        {
            // no-op
        }

        public void KeyUp(byte ascii, VstVirtualKey virtualKey, VstModifierKeys modifers)
        {
            // no-op
        }

        public VstKnobMode KnobMode { get; set; }

        public void Open(IntPtr hWnd)
        {
            //_uiWrapper.SafeInstance.NoteMap = _plugin.NoteMap;
            //_uiWrapper.SafeInstance.NoteOnEvents = _plugin.GetInstance<MidiProcessor>().NoteOnEvents;
            try
            {
                wpfWrapper.Open(hWnd);
            }
            catch(System.InvalidOperationException)
            {
                /*
                 * このクラスを生成したスレッドと別のスレッドから呼び出された場合、
                 * Dispatcher.VerifyAccess()でエラーが発生するため、再度画面を作成する。
                */
                //Amplifier amp = WpfControlWrapper<MainWindow>.Instance.Amp;
                wpfWrapper = new WpfControlWrapper<MainWindow>(Width, Height);
                _plugin.PluginRoot.Window = wpfWrapper.Instance;
                //WpfControlWrapper<MainWindow>.Instance.SetAmplifier(amp);
                wpfWrapper.Open(hWnd);
            }
        }

        public void ProcessIdle()
        {
            //_uiWrapper.SafeInstance.ProcessIdle();
            //_uiWrapper.SafeInstance.
        }

        #endregion
    }
}

